package com.feng.security.config;

import com.feng.common.utils.SecurityUtil;
import com.feng.security.filter.GlobalUserAuthFilter;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.stereotype.Component;

/**
 * @author feng
 * @describe
 */
@Component
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    public void configure(WebSecurity web) throws Exception {
        //放行swagger
        web.ignoring().antMatchers("/swagger-ui.html", "/webjars/**",
                "/swagger-resources/**", "/swagger-ui.html/**", "/v2/**", "/csrf");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //允许跨域请求
        http.cors();
        //移除csrf过滤器
        http.csrf().disable();
        //释放的资源
        http.authorizeRequests()
                .antMatchers(SecurityUtil.RELEASER)
                .permitAll()
                .anyRequest()
                .authenticated();

        //添加自定义拦截器
        http.addFilter(new GlobalUserAuthFilter(authenticationManager()));
    }


}
